<?php
namespace app\techn_api_n\controller\v1;
use app\common\logic\TechnLevelLogic;
use think\Db;
use think\Request;


class Tuisong extends Base
{
	public function _initialize()
	{
        parent::_initialize();
        $this->IsLogin(); //验证登录
        $this->IsTechn(); //验证技师


    }


    public function send(Request $request){

        $techn = $this->techn;
        $order_status = Db::name('order')->where('techn_id',$techn['id'])->where('status','in','1')->where('is_send',0)->find();
        if (!empty($order_status)){
            Db::name('order')->where('id',$order_status['id'])->update(['is_send'=>1]);
            $data['title'] = '接单通知';
            $data['content'] = '您有新订单待接单';
            $return['code'] = 0;
            $return['msg'] = '获取成功';
            $return['data'] = $data;
            echo json_encode($return);
        }

    }
    public function app_level(){
        $info = Db::name('app_level')->find();
        echo  json_encode(['code'=>0,'msg'=>'ok','data'=>$info['app_level']]);
    }

    public function tongji(){
        $techn = $this->techn;
        $day = date('d');
        $qi = 16;
        if ($day < $qi){
            $min = strtotime(date('Y-m-',time()).'1');
            $max =strtotime( date('Y-m-',time()).$qi);
        }else{
            $min = strtotime(date('Y-m-',time()).$qi);
            $max = strtotime(date('Y-',time()).(date('m',time())+1).'-1');
        }
        $where['add_time'] = ['between',$min.','.$max];
        $where['status'] = 6;
        $where['techn_id'] = $techn['id'];
        $order_list = Db::name('order')->where($where)->select();
        $dd['yeji'] = Db::name('order')->where($where)->sum('price');
        $dd['shouru'] = Db::name('balance')
            ->where('add_time','between',$min.','.$max)
            ->where('identity_id',$techn['id'])
            ->where('balance_type',1)
            ->where('identity_type','techn')
            ->sum('yes_money');

//        $dd['shouru'] = '-';
        $level =  Db::name('techn_last')->where('techn_id',$techn['id'])->value('level_id');
        $dd['level'] = 'v'.Db::name('techn_level')->where('id',$level)->value('rank');

        $dd['chefei'] = Db::name('order')->where($where)->sum('travel_price');
        $dd['jzl'] = TechnLevelLogic::jz_v($techn['id'],$min,$max);
        $dd['shichang'] = Db::name('techn_last')->where('techn_id',$techn['id'])->value('total_duration');
        foreach ($order_list as $key=>$value){
            $info = 0;
            if ($value['jztype'] == 1){
                $info = Db::name('order_jzdetail')->where('order_id',$value['id'])->sum('buy_number');
                $info += Db::name('order_upgrade')->where('order_id',$value['id'])->count('id');

            }
            $order_list[$key]['jz_num'] = $info;
            $order_list[$key]['order_num'] = $info+1;
            $order_list[$key]['add_time_time'] = date('Y-m-d',$value['add_time']);



        }
        $dd['order_list'] = $order_list;
        echo json_encode(['code'=>0,'msg'=>'ok','data'=>$dd]);


    }

    public function shijian(){
        $timt = date('Y-m-d H:i',time());
        echo json_encode(['code'=>0,'msg'=>'ok','data'=>$timt]);
    }
    public function paihang(Request $request){
        $techn = $this->techn;

        $day = date('d');
        $qi = 16;
        if ($day < $qi){
            $min = strtotime(date('Y-m-',time()).'1');
            $max =strtotime( date('Y-m-',time()).$qi);
        }else{
            $min = strtotime(date('Y-m-',time()).$qi);
            $max = strtotime(date('Y-',time()).(date('m',time())+1).'-1');
        }
        $where['add_time'] = ['between',$min.','.$max];
        $where['status'] = 6;
        $city = Db::name('proxy')->where('qu',$techn['county'])->where('is_xian',1)->where('is_delete_time',0)->find();
        $my = Db::name('techn')->where('id',$techn['id'])->find();
        $my['self_img_url'] = 'https://h5.hexukeji.com/'.$my['self_img'];
        $where['techn_id'] = $my['id'];
        $my['yeji'] = Db::name('order')->where($where)->sum('price');
        $my['work_img_img'] = 'https://h5.hexukeji.com/'.$my['work_img'];

        $myorder = Db::name('order')->where('techn_id',$my['id'])->select();
        foreach ($myorder as $idk => $idv){
            $idm = 0;
            if ($idv['jztype'] == 1){
                $idm = Db::name('order_jz')->where('order_id',$idv['id'])->where('zftype',1)->sum('price');
                $idm += Db::name('order_upgrade')->where('order_id',$idv['id'])->sum('price');
            }

        }
        $my['jiazhong'] = $idm;
        $idm = Db::name('order_jz')->where('order_id',$my['id'])->sum('price');
        $idm += Db::name('order_upgrade')->where('order_id',$my['id'])->sum('price');
        if (empty($city)){
            $city = Db::name('proxy')->where('shi',$techn['city'])->where('is_delete_time',0)->find();
            $techn_list = Db::name('techn')->where('city',$city['shi'])->where('work_status','<>',3)->select();
        }else{
            $techn_list = Db::name('techn')->where('county',$city['qu'])->where('work_status','<>',3)->select();
        }
        foreach ($techn_list as $key => $value){
            $where['techn_id'] = $value['id'];

            $techn_list[$key]['work_img_img'] = 'https://h5.hexukeji.com/'.$value['work_img'];
            $techn_list[$key]['yeji'] = Db::name('order')->where($where)->sum('price');
            if ($techn_list[$key]['yeji'] == 0){
                unset($techn_list[$key]);
                continue;
            }
            $order_ids = Db::name('order')->where($where)->column('id');

            $idm = Db::name('order_jz')->where('order_id','in',$order_ids)->where('zftype',1)->sum('price');
            $idm += Db::name('order_upgrade')->where('order_id','in',$order_ids)->sum('price');

            $techn_list[$key]['jiazhong'] = $idm;

        }

        $jiazhong = $techn_list;
        $sort = array_column($techn_list,'yeji');
        array_multisort($sort,SORT_DESC,$techn_list);

        $sort = array_column($jiazhong,'jiazhong');
        array_multisort($sort,SORT_DESC,$jiazhong);


        foreach ($techn_list as $mkey => $mvalue){
            if ($mvalue['id'] == $my['id']){
                $my['yeji_no'] = 'No.'.($mkey+1);
            }
        }

        foreach ($jiazhong as $mkey => $mvalue){
            if ($mvalue['id'] == $my['id']){
                $my['jiazhong_no'] = 'No.'.($mkey+1);
            }
        }
        $return = [];
        foreach ($techn_list as $kkk => $vvv){
            $return[$kkk] = $vvv;
            $return[$kkk]['yeji_no'] = 'No.'.($kkk+1);
            $return[$kkk]['yeji'] = '***';
            if ($kkk>8){
                break;
            }
        }

        $jiazhongs = [];
        foreach ($jiazhong as $kkk => $vvv){
            $jiazhongs[$kkk] = $vvv;
            $jiazhongs[$kkk]['jiazhong_no'] = 'No.'.($kkk+1);
            $jiazhongs[$kkk]['jiazhong'] = '***';

            if ($kkk>8){
                break;
            }
        }
        $echo['my'] = $my;
        $echo['yeji'] = $return;
        $echo['jiazhong'] = $jiazhongs;

        echo json_encode(['code'=>0,'msg'=>'ok','data'=>$echo]);
    }



}